#include<iostream>
using namespace std;
const int N = 5e4 + 10;
int p[N];
int d[N];
int n, k;
int find(int x)
{
	if (x != p[x]) {
		int root = find(p[x]);
		d[x] += d[p[x]];
		p[x] = root;
	}
	return p[x];
}
int main()
{
	cin >> n >> k;
	for (int i = 1; i <= n; i++) {
		p[i] = i;
	}
	int res = 0;
	while (k--) {
		int	t, x, y;
		cin >> t >> x >> y;
		if (x > n || y > n) {
			res++;
		}
		else {
			if (t == 1) {
				int px = find(x);
				int py = find(y);
				if (px == py && (d[x] - d[y]) % 3) {
					res++;
				}
				else if (px != py) {
					p[px] = py;
					d[px] = d[y] - d[x];
				}
			}
			else {
				int px = find(x);
				int py = find(y);
				if (px == py && (d[x] - d[y] - 1) % 3) {
					res++;
				}
				else if (px != py) {
					p[px] = py;
					d[px] = d[y] + 1 - d[x];
				}
			}
		}
	}	
	cout << res << endl;
	return 0;
}